﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Company.PackageExtjs.Intellisense
{
    using System.Drawing;
    using System.Windows.Forms;

    public sealed class PropositionList : ListBox
    {
        private ImageList myImageList;
		
        public ImageList ImageList
		{
			get {return this.myImageList;}
			set {this.myImageList = value;}
		}
        
        public PropositionList()
		{
			// Set owner draw mode
			this.DrawMode = DrawMode.OwnerDrawFixed;
		}
		
        protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
		{
			e.DrawBackground();
			e.DrawFocusRectangle();
		    var bounds = e.Bounds;
			var imageSize = this.myImageList.ImageSize;
			try
			{
			    var item = (PropositionListItem)Items[e.Index];
			    if (item.ImageIndex != -1)
				{
					this.myImageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex); 
					e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), 
						bounds.Left+imageSize.Width, bounds.Top);
				}
				else
				{
					e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
						bounds.Left, bounds.Top);
				}
			}
			catch
			{
			    e.Graphics.DrawString(
			        e.Index != -1 ? this.Items[e.Index].ToString() : this.Text,
			        e.Font,
			        new SolidBrush(e.ForeColor),
			        bounds.Left,
			        bounds.Top);
			}
			base.OnDrawItem(e);
		}        
    }
}
